package com.bbzn.device.client.service.impl;

import com.alibaba.fastjson.JSON;
import com.bbzn.device.client.constant.YG;
import com.bbzn.device.client.dto.api.ct.request.RequestCreateProduct;
import com.bbzn.device.client.dto.api.ct.request.RequestQueryProductList;
import com.bbzn.device.client.dto.api.ct.request.RequestUpdateProduct;
import com.bbzn.device.client.service.ProductManagementService;
import com.ctg.ag.sdk.biz.AepProductManagementClient;
import com.ctg.ag.sdk.biz.aep_product_management.*;
import org.springframework.stereotype.Service;


/**
 * @Author wulongbo
 * @Date 2021/9/6 17:13
 * @Version 1.0
 */

@Service
public class ProductManagementServiceImpl implements ProductManagementService {

//    private static AepProductManagementClient client;

//    @PostConstruct
//    public static AepProductManagementClient getClient() {
//        if (client == null) {
//            synchronized (AepProductManagementClient.class) {
//                if (client == null) {
//                    client = AepProductManagementClient.newClient()
//                            .appKey(YG.APP_KEY)
//                            .appSecret(YG.APP_SECRET)
//                            .build();
//                }
//            }
//        }
//        return client;
//    }

    @Override
    public CreateProductResponse createProduct(RequestCreateProduct requestCreateProduct) throws Exception {
        // 1.bean对象转换成byte数组
        byte[] body = JSON.toJSONString(requestCreateProduct).getBytes();
        AepProductManagementClient client = AepProductManagementClient.newClient().appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        CreateProductRequest request = new CreateProductRequest();
        // set your request params here
        request.setBody(body);    //具体格式见前面请求body说明
        CreateProductResponse createProductResponse = client.CreateProduct(request);
        if(null==createProductResponse){
            return null;
        }
        System.out.println(createProductResponse.toString());
        // more request
        client.shutdown();
        return createProductResponse;
    }

    @Override
    public DeleteProductResponse deleteProduct(String masterKey, Integer productId) throws Exception {
        // 1.bean对象转换成byte数组
        //byte[] body = JSON.toJSONString(requestDeleteProduct).getBytes();
        AepProductManagementClient client = AepProductManagementClient.newClient().appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        DeleteProductRequest request = new DeleteProductRequest();
        // set your request params here
        request.setParamMasterKey(masterKey);
        request.setParamProductId(productId);
        DeleteProductResponse deleteProductResponse = client.DeleteProduct(request);
        System.out.println(deleteProductResponse.toString());
        // more request
        client.shutdown();
        return deleteProductResponse;
    }

    @Override
    public QueryProductResponse queryProduct(Integer productId) throws Exception {

        QueryProductRequest request = new QueryProductRequest();
        request.setParamProductId(productId);
        AepProductManagementClient client = AepProductManagementClient.newClient().appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        QueryProductResponse deleteProductResponse = client.QueryProduct(request);
        System.out.println(deleteProductResponse.toString());
        // more request
        client.shutdown();
        return deleteProductResponse;
    }

    @Override
    public QueryProductListResponse queryProductList(RequestQueryProductList requestQueryProductList) throws Exception {

        QueryProductListRequest request = new QueryProductListRequest();
        request.setParamSearchValue(requestQueryProductList.getSearchValue());
        request.setParamPageNow(requestQueryProductList.getPageNow());
        request.setParamPageSize(requestQueryProductList.getPageSize());
        AepProductManagementClient client = AepProductManagementClient.newClient().appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        QueryProductListResponse deleteProductListResponse = client.QueryProductList(request);
        System.out.println(deleteProductListResponse.toString());
        // more request
        client.shutdown();
        return deleteProductListResponse;
    }

    @Override
    public UpdateProductResponse updateProduct(RequestUpdateProduct requestUpdateProduct) throws Exception {
        // 1.bean对象转换成byte数组
        byte[] body = JSON.toJSONString(requestUpdateProduct).getBytes();
        UpdateProductRequest request = new UpdateProductRequest();
        // set your request params here
        request.setBody(body);    //具体格式见前面请求body说明
        AepProductManagementClient client = AepProductManagementClient.newClient().appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        UpdateProductResponse updateProductResponse = client.UpdateProduct(request);
        System.out.println(updateProductResponse.toString());
        // more request
        client.shutdown();
        return updateProductResponse;
    }
}
